package net.adriana.bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.adriana.esaytool.annotation.ColumnName;
import net.adriana.esaytool.util.ConvertUtil;

public class Table {
	@ColumnName("TABLE_CAT")
	private String tableCat;
	@ColumnName("TABLE_SCHEM")
	private String tableSchem;
	@ColumnName("TABLE_NAME")
	private String tableName;
	@ColumnName("TABLE_TYPE")
	private String tableType;
	@ColumnName("REMARKS")
	private String remarks;
	
	private String stdTableName;
	private Map<String, Boolean> extendsClass = new HashMap<String, Boolean>();
	private Map<String, Boolean> methods = new HashMap<String, Boolean>();
	private boolean isSelectedMethods;
	private boolean isSelectedImple;
	private List<String> interfaceNames = new ArrayList<String>();
	private Schema schema;
	
	private Column[] cols;
	
	public Table() {
		super();
	}


	public void init(){
		setStdTableName();
		//默认不继承任何类
		extendsClass.put("", false);
		//默认无接口Serializable
		isSelectedImple = false;
//		interfaceNames.add("Serializable");
		//默认生成toString()方法
		isSelectedMethods = true;
		methods.put("toString()", true);
		methods.put("compareTo()", false);
	}

	
	public Map<String, Boolean> getExtendsClass() {
		return extendsClass;
	}
	public String getExtends() {
		String key = "";
		for (String str : extendsClass.keySet()) {
			key = str;
		}
		return key;
	}
	public void setExtendsClass(String key,boolean value){
		if(!extendsClass.containsKey(key)) extendsClass.clear();
		extendsClass.put(key, value);
	}
	public boolean isExtends(){
		if(extendsClass.isEmpty()) return false;
		return extendsClass.containsValue(true);
	}
	public void setExtendsClass(Map<String, Boolean> extendsClass) {
		this.extendsClass = extendsClass;
	}


	public boolean isSelectedMethods() {
		return isSelectedMethods;
	}


	public void setSelectedMethods(boolean isSelectedMethods) {
		this.isSelectedMethods = isSelectedMethods;
	}


	public boolean isSelectedImple() {
		return isSelectedImple;
	}


	public void setSelectedImple(boolean isSelectedImple) {
		this.isSelectedImple = isSelectedImple;
	}

	public Map<String, Boolean> getMethods() {
		return methods;
	}
	public boolean isSelectedMethods(String ele){
		return methods.get(ele);
	}
	public void setMethods(Map<String, Boolean> methods) {
		this.methods = methods;
	}

	public void setMethods(String key, boolean value){
		if(key.equals("compareTo()") && value){
			if(!isImpleContains("Comparable")) interfaceNames.add("Comparable");
		}else if(key.equals("compareTo()") && !value){
			interfaceNames.remove("Comparable");
		}
		methods.put(key, value);
	}
	
	public boolean isImpleContains(String ele){
		return interfaceNames.contains(ele);
	}
	
	public void removeInterface(String name){
		interfaceNames.remove(name);
	}
	public String[] getColsName(){
		String[] colsName = new String[cols.length];
		for (int i = 0; i < cols.length; i++) {
			colsName[i] = cols[i].getColName();
		}
		return colsName;
	}
	
	public String[] getStdColsName(){
		String[] colsName = new String[cols.length];
		for (int i = 0; i < cols.length; i++) {
			colsName[i] = cols[i].getStdColName();
		}
		return colsName;
	}

	public List<String> getInterfaceNames() {
		return interfaceNames;
	}


	public void setInterfaceNames(List<String> interfaceNames) {
		this.interfaceNames = interfaceNames;
	}

	public void setInterfaceNames(String element) {
		if(!interfaceNames.contains(element))
			interfaceNames.add(element);
	}

	public String getStdTableName() {
		return stdTableName;
	}


	public void setStdTableName() {
		this.stdTableName = ConvertUtil.castToCamelCase(getTableName(), true);
	}

	public String getTableCat() {
		return tableCat;
	}
	public void setTableCat(String tableCat) {
		this.tableCat = tableCat;
	}
	public String getTableSchem() {
		return tableSchem;
	}
	public void setTableSchem(String tableSchem) {
		this.tableSchem = tableSchem;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getTableType() {
		return tableType;
	}
	public void setTableType(String tableType) {
		this.tableType = tableType;
	}
	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	public Column[] getCols() {
		return cols;
	}
	public void setCols(Column[] cols) {
		for (Column column : cols) {
			column.setTable(this);
			column.setSchema(this.schema);
		}
		this.cols = cols;
	}
	public Schema getSchema() {
		return schema;
	}
	public void setSchema(Schema schema) {
		this.schema = schema;
	}


	@Override
	public String toString() {
		return "Table [tableCat=" + tableCat + ", tableSchem=" + tableSchem
				+ ", tableName=" + tableName + ", tableType=" + tableType
				+ ", remarks=" + remarks + ", schema=" + schema + ", cols="
				+ Arrays.toString(getColsName()) + "]";
	}
	
}
